package com.ssm.gbq.controller;

import com.ssm.gbq.model.Order;
import com.ssm.gbq.service.OrderService;
import gbq.ssm.utils.OSSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * 订单Controller
 * @author yafeng
 * 2019年10月9日11:01
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired 
    private OrderService orderService;

    /**
     * 添加新的订单
     * @param order
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addnew", method = RequestMethod.POST)
    public HashMap<String, Object> addnew(Order order){
        HashMap<String,Object> result = new HashMap<String,Object>();
         orderService.insert(order);
        result.put("data","添加成功");
        return result;
    }


    /**
     * 根据订单id删除订单数据
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete_by_id", method = RequestMethod.POST)
    public HashMap<String, Object> deleteById(Integer id){
        HashMap<String,Object> result = new HashMap<String,Object>();
        orderService.deleteById(id);
        result.put("data","删除成功");
        return result;
    }

    /**
     * 根据订单id修改订单数据
     * @param order
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/change_by_id", method = RequestMethod.POST)
    public HashMap<String, Object> changeById( Order order) throws IOException {
        HashMap<String,Object> result = new HashMap<String,Object>();

        orderService.updateById(order);
        result.put("data","更改成功");
        return result;
    }

    /**
     * OSS阿里云上传图片
     */
    //判断上传的图片是否符合规格
    @ResponseBody
    @RequestMapping(value = "/get_img_url")
    public HashMap<String, Object> changeById(MultipartFile file) throws IOException {
        HashMap<String,Object> result = new HashMap<>();
        if (file == null || file.getSize() <= 0) {
            throw new IOException("file不能为空");
        }
        OSSClientUtil ossClient=new OSSClientUtil();
        //生成图片名
        String name = ossClient.uploadImg2Oss(file);
        //根据图片名生成图片url
        String imgUrl = ossClient.getImgUrl(name);
        String[] split = imgUrl.split("\\?");
        result.put("imgUrl",split[0]);
        return result;

    }



    /**
     * 获取订单列表
     * @Title: openTeacherTable
     * @param @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public HashMap<String, Object> getOrderList(){
        HashMap<String,Object> result = new HashMap<String,Object>();
        List<Order> list = orderService.getAll();
        result.put("data",list);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/list_by_limit", method = RequestMethod.POST)
    public HashMap<String, Object> getByLimitPage(Integer currentPage, Integer pageSize){
        HashMap<String,Object> result = new HashMap<String,Object>();
        List<Order> list = orderService.getByLimitPage((currentPage-1)*pageSize,pageSize);
        result.put("data",list);
        return result;
    }

    /**
     * 根据名字查询订单(模糊查询)
     * @param name
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get_by_name", method = RequestMethod.POST)
    public HashMap<String, Object> getByname(String name){
        HashMap<String,Object> result = new HashMap<String,Object>();
        List<Order> list = orderService.getByName(name);
        result.put("data",list);
        return result;
    }

    

}
